package com.smallA.utils;

/**
 * 开启计时器的接口
 */
public class AutoSubmit implements Runnable {
    private int time;
    private String emailAddress;
    private String msg;

    /**
     * 多少分钟后发送邮件
     *
     * @param time         计时器,单位为分钟
     * @param emailAddress 邮箱地址
     */
    public AutoSubmit(int time, String emailAddress) {
        this.time = time;
        this.emailAddress = emailAddress;
        msg = "发送消息测试";
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public void run() {
        long endTime = System.currentTimeMillis() + time * 1000 * 60;
        //获取要结束的时候的毫秒值
        while (true) {
            if (endTime <= System.currentTimeMillis()) {
                if (EmailUtils.sendEmail(emailAddress, msg)) {
                    System.out.println("发送成功");
                } else {
                    System.out.println("发送失败");
                }
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("进程出现问题");
            }
        }
    }
}
